package xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.presenter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import xixirupan.tmgp.tencent.com.vmeim.R;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.model.SubChildrenBean;
import xixirupan.tmgp.tencent.com.vmeim.ui.beautyshop.view.DetailCatgoryActivity;

/**
 * Created by lenovo on 2016/10/19.
 */

public class GridCategoryAdapter extends BaseAdapter {
    private List<SubChildrenBean> subChildren;
    private LayoutInflater inflater;
    private Context context;
    public GridCategoryAdapter(Context contex, List<SubChildrenBean> subChildren, LayoutInflater inflater)
    {
        this.context =contex;
        this.subChildren =subChildren;
        this.inflater =inflater;
    }
    @Override
    public int getCount() {
        return subChildren.size();
    }

    @Override
    public Object getItem(int position) {
        return subChildren.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder ;
        if (convertView ==null)
        {
            convertView =inflater.inflate(R.layout.item_catgory_gridview,parent,false);
            holder =new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.textView_title.setText(subChildren.get(position).getName());
        Glide.with(context).load(subChildren.get(position).getImg()).into(holder.imageView_itemin);
        //子元素的链接
        final String content = subChildren.get(position).getContent();
        String _cid = content.substring(content.indexOf("=") + 1, content.indexOf(','));
         final String cid =_cid.substring(0,_cid.indexOf('"'));
        holder.imageView_itemin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(context,DetailCatgoryActivity.class);
                intent.putExtra("cid",cid);
                context.startActivity(intent);
            }
        });
        return convertView;
    }

class ViewHolder {
    TextView textView_title;
    ImageView imageView_itemin;
    public ViewHolder (View convertView)
    {
        textView_title = (TextView) convertView.findViewById(R.id.item_innercatogry_gridview);

        imageView_itemin = (ImageView) convertView.findViewById(R.id.item_gridview_incategory);
    }
}
}



